package dev.ece.core.light;

import java.util.Iterator;
import java.util.Vector;
/**
 * 光源工厂
 * @author 飘渺青衣
 * <b>E-mail：</b>3375758@qq.com
 */
public class Lights {
	
	private static final Vector<Light> LIGHTS = new Vector<Light>();
	
	static {
		//第一个光源固定是环境光
		getLight(0);
	}
	
	public static void glUniformLights(LightLocationHandler[] lightLocationHandlers) {
		Iterator<Light> it = iterator();
		int index = 0;
		Light light = null;
		while(it.hasNext()) {
			light = it.next();
			if(index < lightLocationHandlers.length) {
				light.glUniformLight(lightLocationHandlers[index ++]);
			} else {
				break;
			}
		}
	}
	
	public static Light getAmbientLight() {
		return getLight(0);
	}
	
	/**
	 * 获得灯光，没有则创建一个灯光
	 * @param index
	 * @return
	 */
	public synchronized static Light getLight(int index) {
		Light light = null;
		if(index < LIGHTS.size()) {
			light = LIGHTS.get(index);
		} else {
			light = new Light();
			//初始化
			light.getAmbient().set(0.6f, 0.6f, 0.6f);
			light.getDiffuse().set(0.8f, 0.8f, 0.8f);
			light.getSpecular().set(1f, 1f, 1f);
			LIGHTS.add(light);
		}
		return light;
	}
	
	public synchronized static void clearLights() {
		//环境光不删除
		Light light = getLight(0);
		LIGHTS.clear();
		LIGHTS.add(light);
	}
	
	public synchronized static Iterator<Light> iterator() {
		return LIGHTS.iterator();
	}
	
	public static int length() {
		return LIGHTS.size();
	}
	
}
